/**
  公用toast弹窗
 */
<template>
  <transition name="toast">
    <div
      class="toast"
      v-show="showToast"
    >
      {{message}}
    </div>
  </transition>
</template>
 <script>
import { defineComponent, reactive, ref, toRefs } from 'vue'

export default defineComponent({
  setup() {
    const state = reactive({
      message: '',
      duration: 2000
    })

    const showToast = ref(false)
    let timer

    const close = () => {
      clearTimeout(timer)
      showToast.value = false
      timer = null
    }

    const open = () => {
      if (timer) clearTimeout(timer)
      showToast.value = true;
      timer = setTimeout(close, state.duration)
      return close
    }

    return {
      ...toRefs(state),
      showToast,
      open,
    }
  }
})
</script>
 
<style lang="scss" scoped>
.toast {
  position: fixed;
  top: 50%;
  left: 50%;
  z-index: 5000;
  padding: 13px 20px;
  border-radius: 4px;
  background-color: rgba(0, 0, 0, 0.8);
  color: #fff;
  text-align: center;
  transform: translate(-50%, -50%);
}

// .demo-enter-active,
// .demo-leave-active {
//   transition: 0.3s ease-out;
// }

.toast-enter {
  opacity: 0;
  transform: scale(1.2);
}

.toast-leave-to {
  opacity: 0;
  transform: scale(0.8);
}
</style>